package service.spider.jdbc;

import com.alibaba.fastjson.JSONObject;
import com.coolpad.job.device.model.GeoFullInfoPO;
import com.coolpad.job.device.model.Point;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class PointBatchUtil {

    public List<GeoFullInfoPO> PointBatchSelect(List<Point> points) throws IOException {
        String location ="";
        for (int i = 0; i <points.size() ; i++) {
            location=location+points.get(i).getX()+","+points.get(i).getY()+"%7C";
        }
        location=location.substring(0,location.length()-3);
        List<GeoFullInfoPO> geoFullInfoPOS= new ArrayList<>();
        String url = "https://restapi.amap.com/v3/geocode/regeo?location=" + location + "&key=47524539c6da2b4d0b753f8b4afb21e0&batch=true";
        System.out.println(url);
        HttpGet httpGet = new HttpGet(url);
        CloseableHttpClient httpClient = HttpClients.createDefault();
        CloseableHttpResponse response = httpClient.execute(httpGet);
        HttpEntity entity = response.getEntity();
        //System.out.println(entity);

        String string = EntityUtils.toString(entity, "utf-8");
        System.out.println(string);

        JSONObject jsonObject = JSONObject.parseObject(string);
        String m = jsonObject.getString("regeocodes");
        List<String> list = JSONObject.parseArray(jsonObject.getString("regeocodes"),String.class);
        long province_id;
        String province_name=null;
        long city_id = 0;
        String city_name=null;
        long county_id = 0;
        String county_name=null;
        long town_id = 0;
        String town_name=null;
        for (int i = 0; i <list.size() ; i++) {
            JSONObject regeocodes = JSONObject.parseObject(list.get(i));
            JSONObject addressComponent = JSONObject.parseObject(regeocodes.getString("addressComponent"));
            //System.out.println("---"+addressComponent);
            if (addressComponent.getString("province").equals("[]")){
                province_name = "";
            }else {
                province_name = addressComponent.getString("province");
            }
            if (addressComponent.getString("citycode").equals("[]")){
                city_id = 0;
            }else {
                city_id = Long.parseLong(addressComponent.getString("citycode"));
            }
            if (addressComponent.getString("city").equals("[]")){
                city_name = "";
            }else {
                city_name = addressComponent.getString("city");
            }
            if (addressComponent.getString("adcode").equals("[]")){
                county_id = 0;
            }else {
                county_id = Long.parseLong(addressComponent.getString("adcode"));
            }
            if (addressComponent.getString("district").equals("[]")){
                county_name = "";
            }else {
                county_name = addressComponent.getString("district");
            }
            if (addressComponent.getString("towncode").equals("[]")){
                town_id = 0;
            }else {
                town_id = Long.parseLong(addressComponent.getString("towncode"));
            }
            if (addressComponent.getString("township").equals("[]")){
                town_name = "";
            }else {
                town_name = addressComponent.getString("township");
            }
            GeoFullInfoPO geoFullInfoPO=new GeoFullInfoPO();
            geoFullInfoPO.setPartitionId(town_id);
            geoFullInfoPO.setLon(String.valueOf(points.get(i).getX()));
            geoFullInfoPO.setLat(String.valueOf(points.get(i).getY()));
            geoFullInfoPO.setProvinceName(province_name);
            geoFullInfoPO.setCityId(city_id);
            geoFullInfoPO.setCityName(city_name);
            geoFullInfoPO.setCountyId(county_id);
            geoFullInfoPO.setCountyName(county_name);
            geoFullInfoPO.setTownId(town_id);
            geoFullInfoPO.setTownName(town_name);
            geoFullInfoPOS.add(geoFullInfoPO);
            //System.out.println(geoFullInfoPO.toString());
        }

        return  geoFullInfoPOS;
    }


}
